var  myPackage = {
	uploadImg(){
		$('body').on('click', '.localImgBtn', function () {
	      var $next = $(this).next('input[type=file]');
	      $next.trigger('click')
	      $next.on('change', function (e) {
	        var files = e.target.files
	        if (!files.length) {
	          return alert('请选择图片')
	        }
	        var file = files[0]
	        var formData = new FormData();
	        formData.append('file', file)
	        //上传formData到接口
	        $.ajax({
                url: ctx + "/prodTpl/uploadPic.html",
                data: formData,
                type: "POST",
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function(){
                    loading.show()
                },
                success: function (data) {
                  loading.hide();
                  console.log(data)
                  //传递完成以后清空input的value
                  e.target.value = '';
                },
                error: function(error) {
                  loading.hide();
                  layer.msg(`${error.statusText}`,{icon:2});
                }
            })
	        //转base64,可用来比较图片是否相同
	        var reader = new FileReader();
	        reader.readAsDataURL(file);
	        // 捕获 转换完毕
	        reader.onload = function(e) {
	          // 转换后的base64就在e.target.result里面,直接放到img标签的src属性即可
	          document.querySelector('img').src = e.target.result
	        }
	      });
	    })
	}
}

/**
 * 转化blob文件下载-只适合excel,其他类型需要改type,暂时不考虑
 * @param {Object} obj
 * @param obj.fileName: 下载下来的文件名称
 * @param obj.url: 需要执行的ajax链接,请求方式指定了必须是post
 * @param obj.formData: new FormData生成的表单数据
 */
function transBlob(obj){
    return new Promise(function(resolve, reject){
        let xhr = new XMLHttpRequest(); //创建请求
        xhr.open('post',obj.url,true); //true表示异步
        xhr.responseType = 'blob'; //设置返回格式
        xhr.send(obj.formData);
        xhr.onload = function(){
            if(xhr.readyState==4 && xhr.status == 200){
                var result = xhr.response;
                const downloadA = document.createElement('a')
                const blob = new Blob([result], { type: 'application/vnd.ms-excel' })
                downloadA.href = window.URL.createObjectURL(blob)
                downloadA.download = obj.fileName;
                downloadA.click();
                window.URL.revokeObjectURL(downloadA.href);
                resolve('下载完成');
            }else{
                reject('请求出错');
            }
        }
    });
}